package com.yaozheng.vocationaltraining.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.CarCategoryListAdapter;
import com.yaozheng.vocationaltraining.bean.CarCategoryBean;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.IBuyCalculatorView;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.BuyCalculatorService;
import com.yaozheng.vocationaltraining.service.impl.BuyCalculatorServiceImpl;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moon on 2017/11/12.
 */

@EFragment(R.layout.activity_buy_calculator)
public class BuyCalculatorActivity extends BaseFragment
        implements SwipeRefreshLayout.OnRefreshListener, ILoadMoreView, IBuyCalculatorView {

    @ViewById
    ListView listView;

    LayoutInflater inflater;
    @ViewById
    TextView headTitleText;

    @Bean(BuyCalculatorServiceImpl.class)
    BuyCalculatorService buyCalculatorService;

    CarCategoryListAdapter carCategoryListAdapter;
    List<CarCategoryBean> carCategoryData = new ArrayList<>();

    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;

    ListViewLoadMoreManage listViewLoadMoreManage;


    int lastId;

    public HomeActivity getHomeActivity() {
        return (HomeActivity) getActivity();
    }

    @AfterViews
    public void initView() {
        headTitleText.setText("购车计算器");
        inflater = this.getActivity().getLayoutInflater();

        buyCalculatorService.init(this);

        listViewLoadMoreManage = new ListViewLoadMoreManage(this, getBaseActivity(),
                listView);
        swipeRefreshLayout.setOnRefreshListener(this);

        carCategoryListAdapter = new CarCategoryListAdapter(getBaseActivity(),
                carCategoryData);
        listView.setAdapter(carCategoryListAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                getHomeActivity().openCarTypeDetailFragment(carCategoryData.get(i).getId(),carCategoryData.get(i).getName());


//                Intent intent = new Intent();
//                intent.setClassName(BuyCalculatorActivity.this.getActivity(),
//                        Constants.CAR_TYPE_DETAIL_ACTIVITY_CLASS_NAME);
//                intent.putExtra("categoryId",carCategoryData.get(i).getId());
//                BuyCalculatorActivity.this.startActivity(intent);
            }
        });


        onRefresh();


    }

    @Override
    public void onRefresh() {
        lastId = 0;
        loadData();
    }

    @Override
    public void loadMoreData() {
        if(carCategoryData != null && carCategoryData.size() > 0){
            int lastId = carCategoryData.get(carCategoryData.size() -1).getId();
            if (lastId > 0) {
                this.lastId = lastId;
                loadData();
            }
        }
    }

    private void loadData() {
        buyCalculatorService.list();
    }


    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        try {
            cancelLoadDataProgressDialog();
            JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","categories");
            if (dataList != null) {
                if (lastId == 0) {
                    carCategoryData.clear();
                }

                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject jsonObject2 = dataList.getJSONObject(i);

                    CarCategoryBean bean = new CarCategoryBean();
                    bean.setId(jsonObject2.getInt("id"));
                    bean.setName(jsonObject2.getString("name"));
                    bean.setIntroduce(jsonObject2.getString("introduce"));
                    bean.setImage(jsonObject2.getString("image"));

                    carCategoryData.add(bean);
                }

                carCategoryListAdapter.setData(carCategoryData);
                carCategoryListAdapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
    }

    @Override
    public void responseError(String error) {

    }
}
